$(function () {
    $('#save-label-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $('#goods-dialog');
        var name = $("input[name='name']").val();
        var coupons_img = $("input[name='coupons_img']").val();
        var price = $("input[name='price']").val();
        var discount = $("input[name='discount']").val();

        // var type_id = document.getElementsByName("type_id")[0].value;
        var submitType = self.attr("data-type");
        var type_id = self.attr('data-id');

        // console.log(type_id);
        var url = '/cms/mall/update_coupons/';

        zlajax.post({
            'url':url,
            'data':{
                'name':name,
                'coupons_img':coupons_img,
                'discount':discount,
                'price':price,
                'coupons_id':type_id,
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code']==200){
                    window.location.reload()
                }else{
                   const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});


$(function () {
    $(".edit-coupons-btn").click(function (event) {
        var self = $(this);
        var dialog = $('#goods-dialog');
        dialog.modal('show');

        var tr = self.parent().parent();
        var name = tr.attr("data-name");
        var coupons_img = tr.attr("data-img");
        // var type_img = tr.attr("data-img");
        var price = tr.attr("data-price");
        var discount = tr.attr("data-discount");

        var nameInput = dialog.find("input[name='name']");
        var imgInput = dialog.find("input[name='coupons_img']");
        var priceInput = dialog.find("input[name='price']");
        var discountInput = dialog.find("input[name='discount']");
        var savebtn = dialog.find("#save-label-btn");

        nameInput.val(name);
        imgInput.val(coupons_img);
        priceInput.val(price);
        discountInput.val(discount);
        savebtn.attr("data-type",'update');
        savebtn.attr("data-id",tr.attr('data-id'));
    })
});


$(function () {
    $('.delete-coupons-btn').click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var banner_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':'您确定要删除这个优惠券吗？',
            'confirmCallback':function () {
                zlajax.post({
                    'url':'/cms/mall/del_coupons/',
                    'data':{
                        'coupons_id':banner_id
                    },
                    'success':function (data) {
                        if(data['code']==200){
                            window.location.reload()
                        }else{
                            const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                        }
                    }
                })
            }
        })
    })
});



